//
//  TBCReadingView.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCReadingView.h"
#import "TBCReading.h"
#import "TBCReadingViewCell.h"

@interface TBCReadingView () <TBCReading, UITableViewDataSource, UITableViewDelegate, ReadingViewCell>

@property (strong, nonatomic) TBCScrollView *scrollViewReading;
@property (strong, nonatomic) BaseTableView *tableViewReading;

@property (strong, nonatomic) NSMutableArray *arrayList;
@property (strong, nonatomic) NSMutableArray *arrayCarousel;

@end

@implementation TBCReadingView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self initTBCReadingData];
        
    }
    
    return self;
    
}

- (void)initTBCReadingViews {
    
    //self.backgroundColor = [UIColor whiteColor];
    
    NSMutableArray *arrayScrollView = [NSMutableArray arrayWithCapacity:0];
    
    for (TBCReading *reading in self.arrayCarousel) {
        
        NSString *img = reading.img;
        [arrayScrollView addObject:img];
        
    }
    
    self.scrollViewReading = [[TBCScrollView alloc] initWithArrayImages:arrayScrollView PlaceHolderImageName:@"PlaceHolderImage@2x"];
    self.scrollViewReading.backgroundColor = [UIColor lightGrayColor];
    
    self.tableViewReading = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableViewReading.delegate = self;
    self.tableViewReading.dataSource = self;
    self.tableViewReading.tableHeaderView = self.scrollViewReading;
    [self addSubview:self.tableViewReading];
    
}

- (void)initTBCReadingData {
    
    [TBCReading shareReading].delegate = self;
    
}

- (void)sendAllArray:(NSMutableArray *)arrayList Array:(NSMutableArray *)arrayCarousel {
    
    self.arrayList = [NSMutableArray arrayWithCapacity:0];
    self.arrayCarousel = [NSMutableArray arrayWithCapacity:0];
    
    self.arrayList = arrayList;
    self.arrayCarousel = arrayCarousel;
    
    [self initTBCReadingViews];
    
}

- (void)pushToController:(NSInteger)typeID {
    
    [self.delegate pushToListViewController:typeID];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return (HEIGHT - 64) * 0.2;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 3;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"reading";
    
    TBCReadingViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        
        cell = [[TBCReadingViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    
    for (int i = 0; i < 9; i++) {
        
        if (i / 3 == indexPath.row) {
            
            [array addObject:self.arrayList[i]];
            
        }
        
    }
    
    cell.array = array;
    cell.delegate = self;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
